/*
* @Author: steven.wang
* @Date:   2019-03-20 16:49:03
* @Last Modified by:   steven.wang
* @Last Modified time: 2019-03-21 11:21:11
*/
const fs = require("fs");
const cheerio = require('cheerio');
const asyncQuene = require("async").queue;
const request = require('superagent');
require('superagent-charset')(request);
const sleep = require('sleep');
const tools = require('./tools');
const config = require('./config');

/**
 * 获取所有画册URL
 * @author steven.wang
 * @DateTime 2019-02-27T11:07:41+0800
 */
const getAlbumsAsync = () => {
    return new Promise((resolve, reject) => {
        console.log('Start get albums .....');
        let albums = [], errors = [];
        let q = asyncQuene(async (pageUrl, cb) => {
            if(albums.length%100 == 1) {
                sleep.sleep(5);
            }
            try {
                let $ = await tools.getHtmlAsync(pageUrl.url, 'utf-8');
                console.log(`download ${pageUrl.url} success`);
                $('#thumbs>.thumb-listing-page>ul>li').each(function (idx, elem) {
                    albums.push({
                        title: $(this).find('.wall-res').text(),
                        url: $(this).find('a.preview').attr('href'),
                        thumbnail: $(this).find('img.lazyload').attr('data-src')
                    });
                });
            } catch (err) {
                errors.push(pageUrl);
                console.log(`Error : get Album list - download ${pageUrl.url} err : ${err}`);
            } finally {
                cb();
            }
        }, config.concurrent);
        /**
         * 监听：当所有任务都执行完以后，将调用该函数
         */
        q.drain = () => {
            console.log('Get album list complete');
            console.log('Get album list err: ' + JSON.stringify(errors));
            tools.writeJsonToFile(config.wallhaven.path + config.wallhaven.endPage + '-' + config.wallhaven.endPage + '-error.json', errors);
            resolve(albums);
        }

        let pageUrls = [];
        for (let i = config.wallhaven.startPage; i < config.wallhaven.endPage; i++) {
            pageUrls.push({
                url: config.wallhaven.host + `?page=${i+1}`
            });
        }
        q.push(pageUrls);
    });
}
/**
 * 获取高清图片
 * @author steven.wang
 * @DateTime 2019-03-21T10:07:37+0800
 * @param    {[type]}                 data [description]
 * @return   {[type]}                      [description]
 */
const getAlbumsHighAsync = data => {
    return new Promise((resolve, reject) => {
        console.log('Start get albums .....');
        let albums = [], errors = [];
        let q = asyncQuene(async (photo, cb) => {
            try {
                if(albums.length%100 == 1) {
                    sleep.sleep(5);
                }
                let $ = await tools.getHtmlAsync(photo.url, 'utf-8');
                console.log(`download ${photo.url} success`);
                albums.push({
                    title: photo.title,
                    url: $('#wallpaper').attr('src'),
                    link: photo.url,
                    thumbnail: photo.thumbnail,
                });
            } catch (err) {
                errors.push(photo);
                console.log(`Error : get Album list - download ${photo.url} err : ${err}`);
            } finally {
                cb();
            }
        }, config.concurrent);
        /**
         * 监听：当所有任务都执行完以后，将调用该函数
         */
        q.drain = () => {
            console.log('Get album list complete');
            console.log('Get album high list err: ' + JSON.stringify(errors));
            tools.writeJsonToFile(config.wallhaven.path + config.wallhaven.endPage + '-' + config.wallhaven.endPage + '-high-error.json', errors);
            resolve(albums);
        }
        q.push(data);
    });
}

async function app() {
    // 下载08绿软站
    let albumList = await getAlbumsAsync();
    albumList = await getAlbumsHighAsync(albumList);
    let values = [];
    for (let item of albumList) {
        values.push([
            '桌面壁纸',
            config.wallhaven.from,
            item.title,
            item.url,
            item.thumbnail
        ]);
    }
    // 写入文件
    tools.writeJsonToFile(config.wallhaven.path + config.wallhaven.endPage + '-' + config.wallhaven.endPage + '.json', albumList);
    // 存入数据库
    let insertResult = await tools.insertPhoto(values);
    console.log(insertResult)
}

app();
